package readwritelock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/*
可重入读写锁降级演示
ps:读锁不能升级为写锁，只有写锁能降级为读锁
 */
public class ReadWriteLockDemo {

  public static void main(String[] args) {

//    创建可重入读写锁
    ReadWriteLock readWriteLockLock = new ReentrantReadWriteLock();
//    读锁
    Lock readLock = readWriteLockLock.readLock();
//    写锁
    Lock writeLock = readWriteLockLock.writeLock();

//    获取写锁
    writeLock.lock();
    System.out.println("获取写锁成功");
//    获取读锁
    readLock.lock();
    System.out.println("获取读锁成功");
//    释放写锁
    writeLock.unlock();
    System.out.println("释放写锁成功");
//    释放读锁
    readLock.unlock();
    System.out.println("释放读锁成功");

  }
}
